package code.starivan.leetcode;

import code.starivan.leetcode.common.TreeNode;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 * Created by Ivan on 2015/10/19.
 */

/*
given a binary tree, return the inorder traversal of its nodes' values.

        For example:
        Given binary tree {1,#,2,3},
        1
        \
        2
        /
        3
        return [1,3,2].

        Note: Recursive solution is trivial, could you do it iteratively?
*/

public class N094_BinaryTreeInorderTraversal {
    public List<Integer> inorderTraversal(TreeNode root) {
        ArrayList<Integer> ret=new ArrayList<>();
        Stack<TreeNode> stack=new Stack<>();
        while (!stack.isEmpty()||root!=null){
            if(root!=null){
                stack.push(root);
                root=root.left;
            }else{
                root=stack.pop();
                ret.add(root.val);
                root=root.right;
            }
        }

        return ret;
    }
}
